<?php

namespace app\partner\controller;


use app\admin\controller\Authorized;
use think\Db;
use think\Session;


class Company extends Authorized
{
    public function _initialize()
    {
        parent::_initialize();

        $my_id=Session::get('admin.id');

        $where=[];
        $where['id']=['>',1];
        $where['state']='0';
        if($my_id>1){
            $where['id']=$my_id;
        }

        $kf_list =Db::table('admin')->where($where)->select();
        $this->assign('kf_list', $kf_list);

    }

    public function index()
    {
        return $this->fetch();
    }


    public function state()
    {
        return $this->fetch();
    }

    public function stop()
    {
        return $this->fetch();
    }


    public function setkf()
    {
        $post_data = $this->request->post();
        if($post_data){

            Db::startTrans();
//          修改客户服务到期时间
            $result =Db::table('company')->where('id', $post_data['company_id'])
                ->setInc('money',$post_data['money']);
            if ($result) {
                Db::commit();// 提交事务

                $post_data['title']='账户充值'.$post_data['money'];
                $post_data['created_time']=time();
                $recordsid =Db::table('company_records')->insertGetId($post_data);
                if ($recordsid) {
                    return json(['code' =>0,'count' =>0,'msg' =>'成功','data'=>'']);
                }else{
                    Db::rollback();// 回滚事务
                    return json(['code' =>1,'count' =>0,'msg' =>'失败','data'=>'']);
                }
            }else{
                Db::rollback();// 回滚事务
                return json(['code' =>1,'count' =>0,'msg' =>'失败','data'=>'']);
            }
        }
        $company=Db::table('company c')->field('c.*,a.username kfname')
            ->join('admin a','c.kf_id=a.id','left')
            ->where('c.id' , input('id'))
            ->find();
        $this->assign('company', $company);


        return $this->fetch();
    }

    public function getList()
    {
        $get_data=$this->request->get();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $kf_id = input('kf_id');
        $keywords = input('keywords');
        $type = input('type');

        $where=[];

        if ($type=='overtime'){
            $where['company.state']=1;
            $where['company.server_endtime']=['< time',time()];
        }elseif ($type=='stop'){
            $where['company.state']=2;
        }elseif ($type=='state'){
            $where['company.state']=0;
        }elseif ($type=='index'){
            $where['company.state']=1;
//            $where['company.server_endtime']=['>= time',time()];
        }

        $my_id=Session::get('admin.id');
        if($my_id>1){
            $where['company.kf_id']=$my_id;
        }

        if (isset($kf_id)){
            if ($kf_id>0){$where['company.kf_id']=$kf_id;}
        }
        if (!empty($keywords)){
            $where['company.title']=['like','%'.$keywords.'%'];
        }

        $count=Db::table('company c')->field('c.*,a.username kfname')
            ->join('admin a','c.kf_id=a.id','left')
            ->where($where)->count();
        $rows=Db::table('company c')->field('c.*,a.username kfname')
            ->join('admin a','c.kf_id=a.id','left')
            ->where($where)
            ->order('c.state,company.id desc')
            ->page($page, $limit)
            ->select();
        return json(['code' =>0,'count' =>$count,'msg'=>'','data'=>$rows]);
    }




    public function add()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result=Db::table('company')->where('name',$post_data['name'])->find();
            if($result){
                return json(['code' =>1,'count' =>0,'msg' =>'注册失败，已存在该用户!','data'=>$result]);
            }else{
                $post_data['created_time']=date('y-m-d h:i:s',time());
                Db::table('company')->startTrans();
                $id =Db::table('company')->insertGetId($post_data);
//            return Db::table('company')->getLastSql();
                if (false === $id) {
                    Db::table('company')->rollback();
                    return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>'']);
                } else {
                    Db::table('company')->commit();
                    return json(['code' =>0,'count' =>0,'msg' =>'添加成功','data'=>'']);
                }
            }


        }


        return $this->fetch();
    }
    public function show()
    {
        $company=Db::table('company')->field('company.*,a.username kfname')
            ->join('admin a','company.kf_id=a.id','left')
            ->where('company.id' , input('id'))
            ->find();
        $this->assign('company', $company);
        return $this->fetch();
    }
    public function edit()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('company')->where('id', $post_data['id'])->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }
        $company=Db::table('company')->field('company.*,a.username kfname')
            ->join('admin a','company.kf_id=a.id','left')
            ->where('company.id' , input('id'))
            ->find();
        $this->assign('company', $company);

        return $this->fetch();
    }


    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result = Db::table('company')->where('id',$post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }
}